package com.atguigu.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


import com.atguigu.gulimall.product.entity.ProductAttrValueEntity;
import com.atguigu.gulimall.product.service.ProductAttrValueService;
import com.atguigu.gulimall.product.vo.AttrResVo;
import com.atguigu.gulimall.product.vo.AttrVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.gulimall.product.entity.AttrEntity;
import com.atguigu.gulimall.product.service.AttrService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;


/**
 * 商品属性
 *
 * @author HX
 * @email 1624242879@qq.com
 * @date 2021-01-17 20:55:42
 */
@ApiModel(description = "商品属性管理")
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;
    @Autowired
    private ProductAttrValueService attrValueService;
    //获取spu规格/product/attr/base/listforspu/{spuId}
    @ApiOperation(value = "获取spu规格")
    @GetMapping("/base/listforspu/{spuId}")
    // @RequiresPermissions("product:attr:list")
    public R listForSpuByspuId(
            @ApiParam(name = "id", value = "spuid", required = true)
            @PathVariable("spuId") Long spuId) {
        List<ProductAttrValueEntity> list = attrValueService.listForSpuByspuId(spuId);
        return R.ok().put("data", list);
    }

    @RequestMapping("/{attrType}/list/{catelogId}")
    // @RequiresPermissions("product:attr:list")
    public R attrlist(@RequestParam Map<String, Object> params,
                      @PathVariable("catelogId") Long catelogId,
                      @PathVariable("attrType") String attrType) {
        PageUtils page = attrService.queryBaseAttrPage(params, catelogId, attrType);
        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    // @RequiresPermissions("product:attr:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    // @RequiresPermissions("product:attr:info")
    public R info(@PathVariable("attrId") Long attrId) {
        //修改时回显所有信息
        AttrResVo attrResVo = attrService.getAttrInfo(attrId);

        return R.ok().put("attr", attrResVo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //  @RequiresPermissions("product:attr:save")
    public R save(@RequestBody AttrVo attrVo) {
        //向attr和attr-group-relation表添加信息
        attrService.saveDetail(attrVo);

        return R.ok();
    }

    //修改商品规格/product/attr/update/{spuId}
    @PostMapping("/update/{spuId}")
    public R updateSpuAttr(@PathVariable("spuId") Long spuId,
                           @RequestBody List<ProductAttrValueEntity> spuAttrs) {
        attrValueService.updateSpuAttrs(spuId, spuAttrs);

        return R.ok();
    }

    /**
     * 修改--------------
     */
    @RequestMapping("/update")
    // @RequiresPermissions("product:attr:update")
    public R update(@RequestBody AttrVo attrVo) {
        attrService.updateDetail(attrVo);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("product:attr:delete")
    public R delete(@RequestBody Long[] attrIds) {
        attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
